const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');
const fs = require('fs');

async function getBotModels(req, res, next) {
  logger.winston.info('Model.getBotModels');

  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from models where bot_id = ?  order by model_id desc",[req.params.bot_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }   
      
  
}

async function createModel(req, res, next) {
  logger.winston.info('Bot.createModel');

    try {
    	
			  const [rows,fields] = await db_promise.query("insert into models(model_name, comment, bot_id, server_path, local_path) values (?,?,?,?,?)", [req.body.file_name, req.body.comment, req.body.bot_id, req.body.server_path, 'Manually added']);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
}

async function removeModel(req, res, next) {
  logger.winston.info('Model.removeModel');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from models where model_id = ?", [req.query.model_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
}

module.exports = {
  getBotModels,
  removeModel,
  createModel
};